package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ApArticleConfigMapper;
import com.zhentao.model.article.pojo.ApArticleConfig;
import com.zhentao.service.ApArticleConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class ApArticleConfigServiceImpl extends ServiceImpl<ApArticleConfigMapper, ApArticleConfig> implements ApArticleConfigService {
    @Override
    public void updateByMap(Map<String, Object> map) {
        // 获取 enable 值，进行类型检查
        Object enable = map.get("enable");
        if (enable == null) {
            log.warn("Enable value is null, articleId: {}", map.get("articleId"));
            return;
        }

        boolean isDown = true;

        try {
            // 确保 enable 为整数类型并进行比较
            if (enable instanceof Integer && (Integer) enable == 1) {
                isDown = false; // 如果 enable == 1，上架即 isDown = false
            } else if (!(enable instanceof Integer)) {
                log.warn("Invalid enable value type: {}, expected Integer. articleId: {}", enable.getClass().getName(), map.get("articleId"));
                return;
            }

            // 确保 articleId 存在
            Object articleId = map.get("articleId");
            if (articleId == null) {
                log.warn("ArticleId is null, unable to update configuration.");
                return;
            }

            // 更新文章配置
            boolean updateSuccess = update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    .eq(ApArticleConfig::getArticleId, articleId)
                    .set(ApArticleConfig::getIsDown, isDown));

            if (updateSuccess) {
                log.info("Successfully updated ApArticleConfig for articleId: {}, isDown: {}", articleId, isDown);
            } else {
                log.error("Failed to update ApArticleConfig for articleId: {}", articleId);
            }

        } catch (Exception e) {
            log.error("Error occurred while updating article configuration for articleId: {}", map.get("articleId"), e);
        }
    }



    //修改文章
//    @Override
//    public void updateByMap(Map map) {
//        //0 下架 1 上架
//        Object enable = map.get("enable");
//        boolean isDown = true;
//        if(enable.equals(1)){//1上架
//            isDown = false;//赋值0   false
//        }
//        //修改文章配置
//        update(Wrappers.<ApArticleConfig>lambdaUpdate().eq(ApArticleConfig::getArticleId,map.get("articleId"))
//                .set(ApArticleConfig::getIsDown,isDown));//不下架就是上架
//
////        //0 下架  1 上架
////        Object enable = map.get("enable");
////        boolean isDown = true;
////        if(enable.equals(1)){
////            isDown = false;
////        }
////
////        //修改文章
////        update(Wrappers.<ApArticleConfig>lambdaUpdate().eq(ApArticleConfig::getArticleId, map.get("articleId"))
////                .set(ApArticleConfig::getIsDown, isDown));
//    }
}
